home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / KOSCHEKA / FILEPEEK.C next >
C/C++ Source or Header  |  1990-06-07  |  4KB  |  153 lines

  1. /********************************/
  2. /* File: FilePeek.c                */
  3. /*                                */
  4. /* Given the sector index into    */
  5. /* an existing file, read the     */
  6. /* sector in and "dump" it to    */
  7. /* the screen                    */
  8. /*                                */
  9. /* Paramters:                    */
  10. /* param0 = file reference    num    */
  11. /*         ( file is open    )        */
  12. /* param1 = sector number         */
  13. /*        (1 sector = 512 bytes)    */
  14. /* ----------------------------    */
  15. /* To Build:                    */
  16. /*                                 */
  17. /* (1) Create a project using    */
  18. /* this file as well as the     */
  19. /* XCMD.Glue.c file. (Set         */
  20. /* project type to     XCMD (or    */
  21. /* XFCN) from the Project menu.    */
  22. /*                                */
  23. /* (2) Bring the project up to    */
  24. /* date.                        */
  25. /*                                */
  26. /* (3) Build Code Resource.     */
  27. /*                                */
  28. /* (4) Use ResEdit to copy the     */
  29. /* resource to your stack.        */
  30. /********************************/
  31.  
  32. #include    <MacTypes.h>
  33. #include    <OSUtil.h>
  34. #include    <MemoryMgr.h>
  35. #include    <FileMgr.h>
  36. #include    <ResourceMgr.h>
  37. #include    <pascal.h>
  38. #include    <strings.h>
  39. #include     "HyperXCmd.h"
  40. #include    "HyperUtils.h"
  41.  
  42.  
  43. #define        SECTOR        512                /*** size of input buffer     ***/
  44. #define        NUMROWS        8                /*** across each line        ***/
  45. #define        NUMLINES    32                /*** lines of data             ***/
  46. #define        NUMBYTES    16                /*** #bytes per row of data    ***/
  47. #define        SPACE        0x020            /*** the space character    ***/
  48.  
  49. pascal void main( paramPtr )
  50.     XCmdBlockPtr    paramPtr;
  51. {
  52.     short        err;
  53.     short        fref;
  54.     short        lc;                /* line count             */
  55.     short        rc;                /* row count            */
  56.     long        cnt;
  57.     long        blk;            /* sector number         */
  58.     Handle        outData;
  59.     short        *rPtr;            /* per row                */
  60.     char        *cPtr;            /* for ASCII            */
  61.     char        *aPtr;            /* points to asciival    */
  62.     char        *buf;
  63.     char        asciiStr[32];
  64.     char        curntLine[256];    
  65.     Str31        numString;
  66.         
  67.     outData = 0L;
  68.     if( paramPtr->paramCount == 2 ){    /*** expect two parameters ***/
  69.         /*** (1) Get our input parameters             ***/
  70.         fref = (short)paramtoNum( paramPtr, 0 );
  71.         blk     = paramtoNum( paramPtr, 1 );        
  72.  
  73.         /*** (2) Read a buffer of data                ***/
  74.         blk = blk * SECTOR;
  75.         err = SetFPos( fref, fsFromStart, blk );
  76.         if ( !err ){
  77.             cnt = SECTOR;
  78.             
  79.             /*** We need to keep this data locked     ***/
  80.             /*** Since we can;t trust callbacks not    ***/
  81.             /*** to move stuff, we allocate the     ***/
  82.             /*** buffer as non-relocatable.            ***/
  83.             buf = NewPtr( cnt );
  84.             err = FSRead( fref, &cnt, buf );
  85.             
  86.             if ( err != noErr &&  err != eofErr ){
  87.                 paramPtr->returnValue = 0L;
  88.                 return;
  89.             }
  90.         }
  91.     
  92.         /*** result is returned to hypercard as     ***/
  93.         /*** a null terminated string                ***/
  94.         outData = NewHandle( 0L );
  95.  
  96.         /*** (3) Start filling the output buffer    ***/
  97.         *curntLine = '\0';
  98.             
  99.         /*** first the number of bytes read in        ***/
  100.         NumToHex( paramPtr, cnt, 4 , &numString );
  101.  
  102.         appendChar( PtoCstr( (char *)&numString), '\r' );
  103.         CopyStrToHandle( &numString, outData );
  104.  
  105.         /*** point to the input data ***/
  106.         rPtr = (short *)buf;
  107.         
  108.         for( lc = 0;  lc < NUMLINES; ++lc ){
  109.             *curntLine = '\0';
  110.  
  111.             /*** blk is the sector address     ***/
  112.             NumToHex( paramPtr, blk, 6, &numString );
  113.             PtoCstr( (char *)&numString );
  114.             strcat( curntLine, &numString );
  115.             appendChar( curntLine, ':' );
  116.             appendChar( curntLine, SPACE );
  117.             aPtr = asciiStr;
  118.             blk += NUMBYTES;
  119.             
  120.             for( rc = 0; rc < NUMROWS; ++rc ){
  121.             
  122.                 /*** convert eight shorts per row        ***/
  123.                 /*** if we overrun the buffer, draw        ***/
  124.                 /*** whatever data follows it...        ***/
  125.                 NumToHex( paramPtr, (long)*rPtr, 4 , &numString);
  126.                 strcat( curntLine, PtoCstr( (char *)&numString));
  127.                 appendChar( curntLine, ' ' );
  128.                 
  129.                 cPtr = (char *)rPtr;
  130.                 aPtr = CopyAscii( aPtr, *cPtr++ );
  131.                 aPtr = CopyAscii( aPtr, *cPtr++ );
  132.                 rPtr++;
  133.             }
  134.             
  135.             *aPtr = '\0';    /*** terminate the ascii data ***/
  136.             strcat( curntLine, asciiStr );
  137.             appendChar( curntLine, '\r' );
  138.             
  139.             /*** move  line into output buffer            ***/
  140.             CopyStrToHandle( curntLine, outData );
  141.         }
  142.         
  143.         DisposPtr( buf );
  144.     }
  145.     
  146.     cnt = GetHandleSize( outData );
  147.     *(*outData + cnt) = '\0';
  148.     paramPtr->returnValue = outData;
  149. }
  150.  
  151.  
  152.  
  153.